/*
 * @Author: 半月狼
 * @Date: 2021-10-25 17:56:17
 * @LastEditor: 半月狼
 * @LastEditTime: 2022-03-03 13:07:19
 * @Description: 
 */

import util from './utils/util';
import upgradePad from './setsrc/upgradePad';
import openVconsole from './setsrc/openVconsole';
import './styles/NlOVS-debug.scss';
import './styles/NlOVS-update.scss';

/**
 * @module NlOVS
 * @class
 * @constructor
 */
class NlOVS {

    constructor($ele, options) {
        this.util = util;

        this.defaultOptions = {};
        this.options = Object.assign({
            platform: 'NL', //AC: 督导系统, NL: 自学系统, 默认是NL
            sub: -1,// 主题:-1-通用、1-早时光书房;2-希和光空间；3-小黑马空间；
            isAPP: false, // 是否在app里
            shopId: '', // 门店id：可选
            upgradePad: false,
            isVconsole: false,
            upgradeApi: null,// 端升级接口
            debug: false, //开发调试模式
            headers: {}
        }, this.defaultOptions, options || {});

        this.parentDom = $ele;
        this.isInNTPAD = this.options.isAPP;
        this.eventArr = {};

        if(this.options.upgradePad){
            console.log('------upgradePad-----');
            upgradePad.call(this);
            return;
        }

        if(this.options.isVconsole){
            console.log('------isVconsole-----');
            openVconsole.call(this);
            return;
        }
    }
    //给播放器实例绑定事件
    on(type, callback){
        let $this = this;
        let isHas = false;
        for(let key in $this.eventArr){
            if(key === type){
                isHas = true;
            }
        }
        if(!isHas) $this.eventArr[type] = [];
        $this.eventArr[type].push(callback);
    }
    off(type, callback){
        let $this = this;
        let isHas = false;
        for(let key in $this.eventArr){
            if(key === type){
                isHas = true;
            }
        }
        if(isHas){
            let typeHandles = $this.eventArr[type];
            if(callback){
                let currentIndex = null;
                for(let i=0,len=typeHandles.length;i<len;i++){
                    let fn = typeHandles[i];
                    if(fn == callback){
                        currentIndex = i;
                        break;
                    }
                }
                if(currentIndex != null && currentIndex >=0){
                    typeHandles.splice(currentIndex, i);
                    $this.eventArr[type] = typeHandles;
                }
            }else{
                typeHandles.length = 0;
                delete $this.eventArr[type];
            }
        }
    }
    //手动触发绑定到播放器实例上的事件
    trigger(type, e, data){
        let $this = this;
        if($this.eventArr[type] && $this.eventArr[type].length > 0){
            $this.eventArr[type].forEach((callback)=>{
                callback(e, data);
            });
        }
    }
    commonEvent(e){
        this.trigger("commonEvent", e);
    }
}

export default NlOVS;

setTimeout(function(){
    var _NlOVS = new NlOVS(document.body, {"isVconsole": true});
}, 2000);